package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// var a = 1  变量定义策略
type VariableDefinitionStrategy struct {
}

func init() {
	RegisterStrategy(&VariableDefinitionStrategy{})
}
func (n VariableDefinitionStrategy) getId() int {
	return lineanalysis.VariableDefinition
}

func (n VariableDefinitionStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	//获取=号的位置
	index := strings.Index(content, "=")
	if index == -1 { //没有等号，说明是变量定义
		store.RegisterVariable(threadStore, structdef.NewVarDefinitionValue(content[3:], "", -1))
		return
	}
	//获取变量名
	variableName := content[:index]
	//去掉开头的var
	variableName = variableName[3:]
	//获取变量值或者表达式
	variableValue := content[index+1:]
	//计算表达式的值
	value := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, variableValue).(string)
	//存储变量
	var variableType = globalfunc.ExecuteFunc("CodeAnalysis", threadStore, value).(int)
	store.RegisterVariable(threadStore, structdef.NewVarDefinitionValue(variableName, value, variableType))
}
